package com.csxh.web.dao.eshop;

import java.io.Serializable;
import java.util.List;

import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.csxh.util.GenericsUtil;
import com.csxh.web.dao.IDao;
import com.csxh.web.dao.mybatis.Mapper;

public abstract class AbstractMybatisDao<T> implements IDao<T> {

	protected Class<T> clazz;
	private SqlSessionFactory sessionFactory;

	
	
	@SuppressWarnings("unchecked")
	public AbstractMybatisDao() {
		super();
		this.clazz=(Class<T>)GenericsUtil.getSuperClassGenricType(this.getClass());
	}

	@Autowired
	public void setSessionFactory(SqlSessionFactoryBean sessionFactory) {
		try {
			this.sessionFactory = sessionFactory.getObject();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	public SqlSessionFactory getSessionFactory() {
		return this.sessionFactory;
	}

	@Override
	public T queryOne(String hql, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> queryAll(String hql, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <P> P queryPropertyOne(String proptyName, String condition, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public <P> List<P> queryPropertyAll(String proptyName, String condition, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getIdProperty() {
		// TODO Auto-generated method stub
		return "id";
	}

	public T findOneById(Serializable id) {

		SqlSession session = sessionFactory.openSession();

		@SuppressWarnings({ "unused", "unchecked" })
		Mapper<T> mapper=(Mapper<T>) session.getMapper(this.clazz);
		
		return mapper.selectOne(id);
		
	}

	public T findOneByCondition(String condition, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	public List<T> findAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findAll(String orderBy) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findAllByCondition(String condition, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<T> findAllByCondition(String orderBy, String condition, Object... params) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long totalCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long totalCount(String condition, Object... params) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public boolean save(T bean) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean delete(Serializable... ids) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean deleteAll() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean update(T bean) {
		// TODO Auto-generated method stub
		return false;
	}

}
